package com.ezandroid.library.image.ext.core.display;

import android.graphics.Bitmap;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.assist.LoadedFrom;
import com.nostra13.universalimageloader.core.imageaware.ImageAware;

public class RoundedVignetteFadeInBitmapDisplayer extends RoundedVignetteBitmapDisplayer {

    private final int durationMillis;

    private final boolean animateFromNetwork;
    private final boolean animateFromDisk;
    private final boolean animateFromMemory;

    public RoundedVignetteFadeInBitmapDisplayer(int cornerRadiusPixels, int durationMillis) {
        this(cornerRadiusPixels, 0, durationMillis, true, true, true);
    }

    public RoundedVignetteFadeInBitmapDisplayer(int cornerRadiusPixels, int durationMillis,
                                                boolean animateFromNetwork, boolean animateFromDisk, boolean animateFromMemory) {
        this(cornerRadiusPixels, 0, durationMillis, animateFromNetwork, animateFromDisk,
                animateFromMemory);
    }

    public RoundedVignetteFadeInBitmapDisplayer(int cornerRadiusPixels, int marginPixels,
                                                int durationMillis) {
        this(cornerRadiusPixels, marginPixels, durationMillis, true, true, true);
    }

    public RoundedVignetteFadeInBitmapDisplayer(int cornerRadiusPixels, int marginPixels,
                                                int durationMillis, boolean animateFromNetwork, boolean animateFromDisk,
                                                boolean animateFromMemory) {
        super(cornerRadiusPixels, marginPixels);
        this.durationMillis = durationMillis;
        this.animateFromNetwork = animateFromNetwork;
        this.animateFromDisk = animateFromDisk;
        this.animateFromMemory = animateFromMemory;
    }

    @Override
    public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {
        super.display(bitmap, imageAware, loadedFrom);

        if ((animateFromNetwork && loadedFrom == LoadedFrom.NETWORK)
                || (animateFromDisk && loadedFrom == LoadedFrom.DISC_CACHE)
                || (animateFromMemory && loadedFrom == LoadedFrom.MEMORY_CACHE)) {
            animate(imageAware.getWrappedView(), durationMillis);
        }
    }

    /**
     * Animates {@link ImageView} with "fade-in" effect
     *
     * @param imageView      {@link ImageView} which display image in
     * @param durationMillis The length of the animation in milliseconds
     */
    public static void animate(View imageView, int durationMillis) {
        if (imageView != null) {
            AlphaAnimation fadeImage = new AlphaAnimation(0, 1);
            fadeImage.setDuration(durationMillis);
            fadeImage.setInterpolator(new DecelerateInterpolator());
            imageView.startAnimation(fadeImage);
        }
    }
}
